const express = require("express")
const router = express.Router()
const shareDB = require("../../../db/share")
const {join} = require("path")
const multer = require("multer")
const path = require("path")

// 分享列表
router.get("/getAll",async(req,res)=>{
  try {
    let doc = await shareDB.find({},{},{
      sort:{
        date:-1
      }
    }).populate("author",{pass:0,allowPublish:0,allowDelete:0,allowUpdate:0,Supertube:0})
    res.send({
      code:0,
      mes:"分享列表数据",
      data:doc
    })
  } catch (error) {
    res.send({
      code:5,
      mes:"服务器异常，请稍后再试"
    })
  }
})

// 删除操作
router.use("/delete",require("./delete"))

// 更新操作
router.use("/update",require("./update"))

// 发表操作
// 鉴权有没有发表权限
router.use((req,res,next)=>{
  if(!req.session.userInfo.allowPublish){
    return res.send({
      code:14,
      mes:"您没有发表权限，请联系超级管理员进行添加"
    })
  }
  next()
})

// 上传md文件
let mdUpload = multer({
  storage:multer.diskStorage({
    // 文件存储的目录
    destination(req,file,cb){
      cb(null,join(__dirname,"../../../static/share/md"))
    },
    // 文件的名字
    filename(req,file,cb){
      let name = req.session.userInfo._id
      let ext = path.extname(file.originalname)
      req.coverFile = "share"+name+Date.now()+ext
      cb(null,req.coverFile)  //名字
    }
  })
}).single("file") //name名字 elemetUI默认为file

// 上传cover文件
let coverUpload = multer({
  storage:multer.diskStorage({
    // 文件存储的目录
    destination(req,file,cb){
      cb(null,join(__dirname,"../../../static/share/cover"))
    },
    // 文件的名字
    filename(req,file,cb){
      let name = req.session.userInfo._id
      let ext = path.extname(file.originalname)
      req.coverFile = "share"+name+Date.now()+ext
      cb(null,req.coverFile)  //名字
    }
  })
}).single("file") //name名字 elemetUI默认为file

// 上传md文件
router.post("/uploadMd",async(req,res)=>{
  try {
    mdUpload(req,res,async err=>{
      if(err){
        return res.send({
          code:8,
          mes:"md文件上传失败,请刷新后重试"
        })
      }  
      res.send({
        code:0,
        mes:"md文件上传成功",
        data:"/share/md/"+req.coverFile
      }) 
    }) 
  } catch (error) {
    console.log(error)
    res.send({
      code:5,
      mes:"服务器异常，请稍后再试"
    })
  }
})

// 上传cover文件
router.post("/uploadCover",async(req,res)=>{
  try {
    coverUpload(req,res,async err=>{
      if(err){
        return res.send({
          code:8,
          mes:"md文件上传失败,请刷新后重试"
        })
      }  
      res.send({
        code:0,
        mes:"cover文件上传成功",
        data:"/share/cover/"+req.coverFile
      }) 
    }) 
  } catch (error) {
    console.log(error)
    res.send({
      code:5,
      mes:"服务器异常，请稍后再试"
    })
  }
})

// 发表分享
router.post("/uploadShare",async(req,res)=>{
  try {
    let {title,des,md,cover} = req.body
    await shareDB.create({
      title,
      des,
      md,
      cover,
      author:req.session.userInfo._id
    })
    res.send({
      code:0,
      mes:"分享发表成功"
    })
  } catch (error) {
    console.log(error)
    res.send({
      code:5,
      mes:"服务器异常，请稍后再试"
    })
  }
})


module.exports = router